package cn.sytton.taffe.framework.security.handler;

import cn.sytton.taffe.framework.log.LogWriter;
import cn.sytton.taffe.framework.response.ResponseResult;
import cn.sytton.taffe.framework.security.jwt.JwtFactory;
import cn.sytton.taffe.framework.util.JsonUtil;
import cn.sytton.taffe.framework.util.ResponseWriter;
import cn.sytton.taffe.framework.security.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义退出处理类 返回成功
 *
 * @author skyrock
 */
@Component
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler
{
    @Resource
    private JwtFactory jwtFactory;

    @Resource
    private LogWriter logWriter;

    /**
     * 退出处理
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        LoginUser loginUser = jwtFactory.getLoginUser(request);
        if (loginUser != null) {
            // 删除用户缓存记录
            jwtFactory.delLoginUser(loginUser.getTokenId());
//            logWriter.logBegin(request, handler);
//            logWriter.logEnd("");
            //todo 记录用户退出日志
            //AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGOUT, "退出成功"));
        }
        ResponseWriter.print(response, 200, JsonUtil.getString(ResponseResult.success()));
    }
}
